// pages/login/login.js
const app = getApp();
import httpsUtils from '../../utils/https.js';
import myUtils from '../../utils/util.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hint: '请输入手机号',
    result: '',
    codeResult: '',
    codeHint: '请输入验证码',
    verifyCodeTime: '获取验证码',
    buttonDisable: false,
    backStep: 1,
    isTab: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      backStep: options.backStep,
      isTab: options.isTab,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //事件函数
  /* 手机号输入框 */
  bindInputBlur: function (e) {
    var that = this;
    var PhoneReg = new RegExp('^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$');
    var phones = PhoneReg.exec(e.detail.value);

    if (myUtils.isMobile(e.detail.value)) {
      that.setData({
        result: e.detail.value,
        hint: '请输入手机号'
      })
    } else {
      that.setData({
        result: '',
        hint: '手机号有误，请重新输入'
      })
    }
  },
  //获取验证码
  resetCode: function (e) {
    var that = this;
    setTimeout(function () {
      var phones = that.data.result;
      if (!myUtils.isMobile(phones)) {
        wx.showToast({
          title: '请输入正确手机',
          icon: 'none'
        })
        return;
      }

      if (that.data.buttonDisable) return false;
      var c = 88;
      var intervalId = setInterval(function () {
        c = c - 1;
        that.setData({
          verifyCodeTime: c + 's后重发',
          buttonDisable: true
        })
        if (c == 0) {
          clearInterval(intervalId);
          that.setData({
            verifyCodeTime: '获取验证码',
            buttonDisable: false
          })
        }
      }, 1000)


      wx.showToast({
        title: phones,
      })

      wx.request({
        url: app.globalData._url + '/user/getPhoneCode',
        method: 'get',
        header: {
          'content-type': 'application/json;charset=UTF-8'
        },
        data: {
          phone: phones,
          type: 1
        },
        success: function (res) {
        }
      })
    },1000)
  },
  bindVeriCode: function (e) {
    var that = this;
    that.setData({
      codeResult: e.detail.value,
    })
  },
  formSubmit: function (e) {
    var that = this;
    const client_phone = e.detail.value['clientPhone'];
    const veri_code = e.detail.value['veriCode'];

    if (!myUtils.isMobile(client_phone) || myUtils.isBlank(client_phone) || myUtils.isBlank(veri_code) || veri_code.length != 6) {
      wx.showToast({
        title: '请输入正确信息',
        icon:'none'
      })
      return;
    }

    httpsUtils.postData({
      _url: app.globalData._url + '/user/xcxToLoginByMessage',
      data: {
        phone: client_phone,
        code: veri_code
      },
    }, function (json) {
      if (json.success) {
        wx.setStorageSync('userInfo', json.data);
        wx.setStorageSync('utoken', json.data.token);
        app.globalData.token = json.data.token;

        if (that.data.isTab || that.data.isTab == 'true') {
          var delta = parseInt(that.data.backStep);
          wx.navigateBack({
            delta: delta,
            success: function () {
              wx.showToast({
                title: '登陆成功',
              })
            },
          })
        } else {
          wx.switchTab({
            url: '../myinfo/myinfo',
            success: function () {
              wx.showToast({
                title: '登陆成功',
              })
            },
          })
        }

      } else {
        wx.showToast({
          title: json.msg,
        })
      }
    })
  },
})